//
//  CANewAddressView.m
//  LNH
//
//  Created by twentylx on 2018/1/29.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CANewAddressView.h"
#import "UITextView+CAPlaceholder.h"

@implementation CANewAddressView 

/**
 为视图绑定 viewModel
 
 @param viewModel 要绑定的ViewModel
 @param params 额外参数
 */
- (void)bindViewModel:(id <LMViewProtocol>)viewModel withParams:(NSDictionary *)params {
    
}

/**
 初始化额外数据
 */
- (void)lm_initializeForView {
    self.backgroundColor = [UIColor clearColor];
}

/**
 初始化视图
 */
- (void)lm_createViewForView {
    [self setNeedsUpdateConstraints];
    
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.nameTextField];
    [self.bgView addSubview:self.lineView];
    [self.bgView addSubview:self.phoneTextField];
    [self.bgView addSubview:self.line1View];
    [self.bgView addSubview:self.addressView];
    [self.addressView addSubview:self.addressTextField];
//    [self.addressView addSubview:self.moreImageView];
    [self.bgView addSubview:self.line2View];
    [self.bgView addSubview:self.addressDetailTextView];
    
    [self addSubview:self.defaultView];
    [self addSubview:self.defaultLabel];
    [self addSubview:self.scitchButton];
}

#pragma mark layout
- (void)updateConstraints {
    [super updateConstraints];
    
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self);
        make.top.equalTo(self);
        make.height.mas_equalTo(200);
    }];
    
    [_nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.bgView);
        make.top.equalTo(self.bgView);
        make.height.mas_equalTo(40);
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.bgView);
        make.top.equalTo(self.nameTextField.mas_bottom);
        make.height.mas_equalTo(ONE_PIXEL);
    }];
    
    [_phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.bgView);
        make.top.equalTo(self.lineView.mas_bottom);
        make.height.mas_equalTo(40);
    }];
    
    [_line1View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.bgView);
        make.top.equalTo(self.phoneTextField.mas_bottom);
        make.height.mas_equalTo(ONE_PIXEL);
    }];
    
    [_addressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self);
        make.top.equalTo(self.line1View.mas_bottom);
        make.height.mas_equalTo(40);
    }];
    
    [_addressTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.addressView);
    }];
    
//    [_moreImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.addressView);
//        make.right.equalTo(self.addressView).offset(-20);
//    }];
    
    [_line2View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.bgView);
        make.top.equalTo(self.addressView.mas_bottom);
        make.height.mas_equalTo(ONE_PIXEL);
    }];
    
    [_addressDetailTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.bgView);
        make.top.equalTo(self.line2View.mas_bottom);
        make.height.mas_equalTo(80);
    }];
    
    [_defaultView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self);
        make.top.equalTo(self.bgView.mas_bottom).offset(20);
        make.height.mas_equalTo(60);
    }];
    
    [_defaultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.defaultView).offset(20);
        make.centerY.equalTo(self.defaultView);
    }];
    
    [_scitchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.defaultView.mas_right).offset(-20);
        make.centerY.equalTo(self.defaultView);
    }];
}

#pragma mark get & set
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

- (UITextField *)nameTextField {
    if (!_nameTextField) {
        _nameTextField = [UITextField new];
        _nameTextField.placeholder = @"收货人";
        [_nameTextField setValue:RGBCOLOR(178, 178, 178) forKeyPath:@"_placeholderLabel.textColor"];
        _nameTextField.font = [UIFont systemFontOfSize:16];
        
        UIView *leftView = [[UIView alloc] initWithFrame:(CGRect){0, 0, 20, 20}];
        _nameTextField.leftView = leftView;
        _nameTextField.leftViewMode = UITextFieldViewModeAlways;
    }
    return _nameTextField;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = RGBCOLOR(241, 241, 241);
    }
    return _lineView;
}

- (UITextField *)phoneTextField {
    if (!_phoneTextField) {
        _phoneTextField = [UITextField new];
        _phoneTextField.placeholder = @"联系电话";
        [_phoneTextField setValue:RGBCOLOR(178, 178, 178) forKeyPath:@"_placeholderLabel.textColor"];
        _phoneTextField.font = [UIFont systemFontOfSize:16];
        
        UIView *leftView = [[UIView alloc] initWithFrame:(CGRect){0, 0, 20, 20}];
        _phoneTextField.leftView = leftView;
        _phoneTextField.leftViewMode = UITextFieldViewModeAlways;
    }
    return _phoneTextField;
}

- (UIView *)line1View {
    if (!_line1View) {
        _line1View = [UIView new];
        _line1View.backgroundColor = RGBCOLOR(241, 241, 241);
    }
    return _line1View;
}

- (UIView *)addressView {
    if (!_addressView) {
        _addressView = [UIView new];
        _addressView.backgroundColor = [UIColor clearColor];
    }
    return _addressView;
}

- (UITextField *)addressTextField {
    if (!_addressTextField) {
        _addressTextField = [UITextField new];
        _addressTextField.placeholder = @"所在地区";
        [_addressTextField setValue:RGBCOLOR(178, 178, 178) forKeyPath:@"_placeholderLabel.textColor"];
        _addressTextField.font = [UIFont systemFontOfSize:16];
        
        UIView *leftView = [[UIView alloc] initWithFrame:(CGRect){0, 0, 20, 20}];
        _addressTextField.leftView = leftView;
        _addressTextField.leftViewMode = UITextFieldViewModeAlways;
    }
    return _addressTextField;
}

- (UIImageView *)moreImageView {
    if (!_moreImageView) {
        _moreImageView = [UIImageView new];
        _moreImageView.image = LMImageNamed(@"more");
    }
    return _moreImageView;
}

- (UIView *)line2View {
    if (!_line2View) {
        _line2View = [UIView new];
        _line2View.backgroundColor = RGBCOLOR(241, 241, 241);
    }
    return _line2View;
}

- (UITextView *)addressDetailTextView {
    if (!_addressDetailTextView) {
        _addressDetailTextView = [UITextView new];
        _addressDetailTextView.ca_placeholder = @"请填写详细地址";
        _addressDetailTextView.ca_placeholderColor = RGBCOLOR(178, 178, 178);
        _addressDetailTextView.textContainerInset =  UIEdgeInsetsMake(5, 20, 0, 0);
//        [_addressDetailTextView setValue:RGBCOLOR(178, 178, 178) forKeyPath:@"_placeholderLabel.textColor"];
        _addressDetailTextView.font = [UIFont systemFontOfSize:16];
        
//        UIView *leftView = [[UIView alloc] initWithFrame:(CGRect){0, 0, 20, 20}];
//        _addressDetailTextView.leftView = leftView;
//        _addressDetailTextView.leftViewMode = UITextFieldViewModeAlways;
    }
    return _addressDetailTextView;
}

- (UIView *)defaultView {
    if (!_defaultView) {
        _defaultView = [UIView new];
        _defaultView.backgroundColor = [UIColor whiteColor];
    }
    return _defaultView;
}

- (UILabel *)defaultLabel {
    if (!_defaultLabel) {
        _defaultLabel = [UILabel new];
        _defaultLabel.font = [UIFont systemFontOfSize:16];
        _defaultLabel.text = @"设为默认";
    }
    return _defaultLabel;
}

- (UISwitch *)scitchButton {
    if (!_scitchButton) {
        _scitchButton = [[UISwitch alloc] initWithFrame:CGRectZero];
        _scitchButton.onTintColor = RGBCOLOR(233, 85, 19);
    }
    return _scitchButton;
}

@end
